package com.jzh.cmhSystem.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Claim;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {

    /**
     * 有效期为 24 小时
     */
    public static final Long JWT_TTL = 60 * 60 * 1000 * 24L;
    /**
     * 设置秘钥明文
     */
    public static final String JWT_KEY = "jwt_travel";

    public static String genToken(Map<String, Object> claims) {
        com.auth0.jwt.JWTCreator.Builder tokenBuilder = JWT.create();

        // 添加所有的 claims
        for (Map.Entry<String, Object> entry : claims.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Boolean) {
                tokenBuilder.withClaim(key, (Boolean) value);
            } else if (value instanceof Integer) {
                tokenBuilder.withClaim(key, (Integer) value);
            } else if (value instanceof Long) {
                tokenBuilder.withClaim(key, (Long) value);
            } else if (value instanceof Double) {
                tokenBuilder.withClaim(key, (Double) value);
            } else if (value instanceof String) {
                tokenBuilder.withClaim(key, (String) value);
            } else if (value instanceof Date) {
                tokenBuilder.withClaim(key, (Date) value);
            } else {
                throw new IllegalArgumentException("Unsupported claim type: " + value.getClass());
            }
        }

        return tokenBuilder
                .withExpiresAt(new Date(System.currentTimeMillis() + JWT_TTL)) // 过期时间
                .sign(Algorithm.HMAC256(JWT_KEY));
    }

    public static Map<String, Object> parseToken(String token) {
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(JWT_KEY))
                .build()
                .verify(token);

        Map<String, Claim> claims = decodedJWT.getClaims();
        Map<String, Object> result = new HashMap<>();

        for (Map.Entry<String, Claim> entry : claims.entrySet()) {
            Claim claim = entry.getValue();
            if (claim.asBoolean() != null) {
                result.put(entry.getKey(), claim.asBoolean());
            } else if (claim.asInt() != null) {
                result.put(entry.getKey(), claim.asInt());
            } else if (claim.asLong() != null) {
                result.put(entry.getKey(), claim.asLong());
            } else if (claim.asDouble() != null) {
                result.put(entry.getKey(), claim.asDouble());
            } else if (claim.asString() != null) {
                result.put(entry.getKey(), claim.asString());
            } else if (claim.asDate() != null) {
                result.put(entry.getKey(), claim.asDate());
            } else {
                result.put(entry.getKey(), claim.as(Object.class)); // as(Object.class) 用于获取其他类型的值
            }
        }

        return result;
    }
}
